﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServisniList.Core;

namespace ServisniList
{
    public partial class Materijali : Form
    {

        #region Fields

        private DataModelDataContext context = null;

        #endregion

        #region Constructor
        
        public Materijali()
        {
            InitializeComponent();
            context = new DataModelDataContext(ConfigurationManager.ConnectionStrings["ServisniListConnectionString"].ConnectionString);
            materijalBindingSource.DataSource = context.Materijals.ToList();
        } 
        
        #endregion

        #region Event Handlers
        
        private void Materijali_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (context != null)
            {
                context.Dispose();
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

        private void btnSnimi_Click(object sender, EventArgs e)
        {
            this.Update();
            context.SubmitChanges();
        } 

        #endregion

        private void btnSnimi_Click_1(object sender, EventArgs e)
        {
            MaterijalNovi mNovi = new MaterijalNovi();
            mNovi.ShowDialog();
            if (mNovi.UspjesnoKreiranaNovi)
            {
                materijalBindingSource.DataSource = context.Materijals.ToList();
            }
        }
    }
}
